package com.bestvike.stone.core.util.area;

import com.bestvike.stone.core.io.StreamReader;
import com.bestvike.stone.core.serialization.JsonSerializer;
import lombok.extern.apachecommons.CommonsLog;

import java.io.InputStream;
import java.nio.charset.StandardCharsets;

/**
 * Created by 许崇雷 on 2018/8/1.
 */
@CommonsLog
public final class AreaUtils {
    private static final ProvinceCollection PROVINCE_COLLECTION;

    static {
        ProvinceCollection provinceCollection = null;
        try (InputStream resourceAsStream = AreaUtils.class.getResourceAsStream("area.json");
             StreamReader reader = new StreamReader(resourceAsStream, StandardCharsets.UTF_8)) {
            String json = reader.readToEnd();
            provinceCollection = JsonSerializer.deserialize(json, ProvinceCollection.class);
        } catch (Exception e) {
            log.error("Init area error", e);
        }
        PROVINCE_COLLECTION = provinceCollection;
    }

    private AreaUtils() {
    }

    public static ProvinceCollection getProvinceCollection() {
        return PROVINCE_COLLECTION;
    }

    public static Province getProvince(String code) {
        return PROVINCE_COLLECTION.getProvince(code);
    }

    public static City getCity(String code) {
        return PROVINCE_COLLECTION.getCity(code);
    }

    public static County getCounty(String code) {
        return PROVINCE_COLLECTION.getCounty(code);
    }

    public static Town getTown(String code) {
        return PROVINCE_COLLECTION.getTown(code);
    }
}
